<?php
class Ext_Config
{
	/**
	 * @var Ext_Property
	 */
	protected $_properties;
	protected $_data;

	public function __construct($class)
	{
		if(!class_exists($class))
			throw new Exception('Invalid property class ' . $class);
		
		$this->_properties = call_user_func(array($class , 'getInstance'));
		
		if(!$this->_properties instanceof Ext_Property)
			throw new Exception('Invalid property implementation ' . $class);
	}
	
	public function getExtends(){
		return  $this->_properties->getExtends();
	}

	public function __get($name)
	{
		if($name === 'xtype')
			return $this->_properties->getXtype();
			
		if($name === 'extend')
			return $this->_properties->getExtend();	
			
		if(!is_array($this->_data) || !$this->_properties->isValid($name) || ! array_key_exists(( string ) $name , $this->_data , true))
			throw new Exception('Trying to get undefined property ' . get_called_class() . '->' . $name);
		
		return $this->_data[$name];
	}

	public function __set($name , $value)
	{
		if(!$this->_properties->isValid($name))
			throw new Exception('Trying to set undefined property ' . get_called_class() . '->' . $name);
		
		return $this->_data[$name] = $value;
	}
	/**
	 * Check if property exists
	 * @param string $name
	 * @return boolean
	 */
	public function isValidProperty($name){
		return $this->_properties->isValid($name);
	}
	
	public function __toArray($exceptEmpty = false)
	{
		$properties = $this->_properties->getList();
		$result = array();
		
		if(empty($properties))
			return array();
			
		foreach($properties as $name => $type)
		{
			if($exceptEmpty && !isset($this->_data[$name]))
				continue;
				
			$result[$name] = '';	
				
			if(isset($this->_data[$name]) && strlen($this->_data[$name]))
			{
				$val =  $this->_data[$name];

				switch($type){
					case Ext_Property::Boolean: 
							if($val)
								$val = true;
							else 
								$val = false;
							break;
					case Ext_Property::Number:
					case Ext_Property::Numeric: 
							$val = floatval($val);
							break;
					case Ext_Property::String: 	
							$val = strval($val);
							break;
				}
				$result[$name] = $val;
			}	
		}
		return $result;
	}
	public function __toString()
	{
		$properties = $this->_properties->getList();
		//$properties['xtype'] = $this->_properties->getXtype();
		$result = array();
		
		if(empty($properties))
			return array();
			
		foreach($properties as $name => $type)
		{
			if(!isset($this->_data[$name]))
				continue;
				
				
			if(isset($this->_data[$name]) && strlen($this->_data[$name]))
			{
				$val =  $this->_data[$name];

				switch($type){
					case Ext_Property::Boolean: 
							if($val)
								$val = true;
							else 
								$val = false;
							break;
					case Ext_Property::Number:
					case Ext_Property::Numeric: 
							$val = floatval($val);
							break;
					case Ext_Property::String: 	
							$val = '"'.strval($val).'"';
							break;
				}
				$result[] = $name.":".$val;
			}	
		}
		return '{'.implode(",\n", $result).'}';
	}
}